<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class NewsDB  extends DBReader{
    private $sql_param = 'new_id, category_id, source_id, title, keywords, description, status, summary, published, update_time, content, tag, picture, add_user_id, edit_user_id, recommend, recommend_index';

    //根据new_id 获取new 信息
    public function get($new_id){

        $sql = 'select '.$this->sql_param.' from news where new_id = :new_id';

        return $this->db()->getRow($sql,array('new_id'=>$new_id));
    }

    //获取cate 列表<带分页>
    public function getNewList($curr_page,$per_page,$filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['title'])){
            $where .= ' and title like :title';
            $bindarr['title'] = '%'.$filter['title'].'%';
        }

        if(isset($filter['status'])){
            $where .= ' and status like :status';
            $bindarr['status'] = '%'.$filter['status'].'%';
        }

        $ret = $this->pager($curr_page, $per_page)
            ->ct('select count(new_id) from news where ' . $where , $bindarr)
            ->rs('select '.$this->sql_param.' from news where '.$where.' order by new_id desc',$bindarr);

        return $ret;
    }

    //添加资讯信息
    public function addNew($category_id, $source_id, $title, $keywords, $description, $status = 1, $summary, $content, $tag, $picture, $add_user_id, $edit_user_id, $recommend = 1, $recommend_index = 1){
        $sql = "insert into news(category_id, source_id, title, keywords, description, status, summary, published, update_time, content, tag, picture, add_user_id, edit_user_id, recommend, recommend_index) values(:category_id, :source_id, :title, :keywords, :description, :status, :summary, now(), now(), :content, :tag, :picture, :add_user_id, :edit_user_id, :recommend, :recommend_index)";

        $bindarr = array(
            'category_id'=>$category_id,
            'source_id'=>$source_id,
            'title'=>$title,
            'keywords'=>$keywords,
            'description'=>$description,
            'status'=>$status,
            'summary'=>$summary,
            'content'=>$content,
            'tag'=>$tag,
            'picture'=>$picture,
            'add_user_id'=>$add_user_id,
            'edit_user_id'=>$edit_user_id,
            'recommend'=>$recommend,
            'recommend_index'=>$recommend_index
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //删除来源信息
    public function delNew($new_id){

        $sql = "delete from news where new_id = :new_id";

        $bindarr = array(
            'new_id'=>$new_id,
        );
        $rs = $this->db()->execute($sql,$bindarr);

        return $rs->rowCount();
    }
}